// pages/shop-cart/index.js
const WXAPI = require("../../api/api");
const baseUrl = require("../../api/config").dev; //这里用的是ES6的写法
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgBaseUrl: `${baseUrl["imgBaseUrl"]}`,
    tips: [
      "双十一全场大促，部分商品7折销售！",
      "春季爆款来临，咨询电话：05512211222",
      "购物车部分商品已过期！",
    ],
    indicatorDots: false,
    autoplay: true,
    interval: 2000,
    duration: 500,
    resultIds: [],
    total: 0,
    allGoodsIdsArr: [],
    list: [],
    baseImgUrl: `${baseUrl["baseUrl"]}/files/`,
    form: {
      sName: "",
      iStatus: 4,
    },
    checkAll: [],
    currentMode: "1",
  },
  toGoodsDetail(e){
    const {id,title} = e.currentTarget.dataset
    wx.navigateTo({
      url: `/packageA/pages/smart-detail/index?id=${id}&title=${title}`,
    })
  },
  onChange(event) {
    // 筛选所选商品对象
    let arr = [];
    let resultArr = [];
    let ids = this.data.list;
    let total = 0;
    for (let i = 0; i < ids.length; i++) {
      for (let j = 0; j < ids[i].ProductGoodsShoppingCarts.length; j++) {
        arr.push(ids[i].ProductGoodsShoppingCarts[j]);
        if (event.detail && event.detail.length > 0) {
          for (let q = 0; q < event.detail.length; q++) {
            if (
              event.detail.indexOf(ids[i].ProductGoodsShoppingCarts[j].sId) !=
              -1
            ) {
              ids[i].ProductGoodsShoppingCarts[j].checked = true;
            } else {
              ids[i].ProductGoodsShoppingCarts[j].checked = false;
            }
          }
        } else {
          ids[i].ProductGoodsShoppingCarts[j].checked = false;
        }
      }
    }
    for (let i = 0; i < arr.length; i++) {
      for (let j = 0; j < event.detail.length; j++) {
        if (arr[i].sId == event.detail[j]) {
          resultArr.push(arr[i]);
          total += arr[i].dPrice * arr[i].iGoodsCount;
        }
      }
    }
    this.setData({
      total,
      list: ids,
      resultIds: event.detail,
      resultArr,
    });
    if (
      event.detail &&
      event.detail.length == this.data.allGoodsIdsArr.length
    ) {
      this.setData({
        checkAll: ["1"],
      });
    } else {
      this.setData({
        checkAll: [],
      });
    }
  },
  addNum(e){
    console.log('e',e);
  },
  reduceNum(e){},
  click(){},
  addGoodsNum(e) {
    const { id } = e.currentTarget.dataset;
    let ids = this.data.list;
    let arr = this.data.resultArr;
    let total = 0;
    for (let i = 0; i < ids.length; i++) {
      for (let j = 0; j < ids[i].ProductGoodsShoppingCarts.length; j++) {
        if (ids[i].ProductGoodsShoppingCarts[j].sGoodsId == id) {
          ids[i].ProductGoodsShoppingCarts[j].iGoodsCount = e.detail;
        }
      }
    }
    if (arr && arr.length > 0) {
      for (let i = 0; i < arr.length; i++) {
        if (arr[i].sGoodsId == id) {
          arr[i].iGoodsCount = e.detail;
        }
        total += arr[i].dPrice * arr[i].iGoodsCount;
      }
    }

    this.setData({
      total,
      list: ids,
      resultArr: arr,
    });
  },
  openFilter() {
    this.setData({
      currentMode: this.data.currentMode == "1" ? "2" : "1",
    });
  },
  selectAll(e) {
    let ids = this.data.list;
    let resultIds = [];
    let resultArr = [];
    let total = 0;
    for (let i = 0; i < ids.length; i++) {
      for (let j = 0; j < ids[i].ProductGoodsShoppingCarts.length; j++) {
        if (e.detail && e.detail.length > 0) {
          ids[i].ProductGoodsShoppingCarts[j].checked = true;
          resultIds.push(ids[i].ProductGoodsShoppingCarts[j].sId);
          resultArr.push(ids[i].ProductGoodsShoppingCarts[j]);
          total +=
            ids[i].ProductGoodsShoppingCarts[j].dPrice *
            ids[i].ProductGoodsShoppingCarts[j].iGoodsCount;
        } else {
          ids[i].ProductGoodsShoppingCarts[j].checked = false;
        }
      }
    }
    this.setData({
      total,
      list: ids,
      checkAll: e.detail,
      resultIds,
      resultArr,
    });
  },
  toInitOrder() {
    console.log("resultArr", this.data.resultArr);
    wx.navigateTo({
      url: `/packageA/pages/order-pay/index?goodsArrStr=${JSON.stringify(
        this.data.resultArr
      )}`,
    });
  },
  del() {
    let _this = this;
    wx.showModal({
      title: "提示",
      content: `确认删除？`,
      async success(res) {
        if (res.confirm) {
          let result = await WXAPI.delShopCar(_this.data.resultIds);
          if (result.data.code == 0) {
            _this.getList();
          }
        }
      },
    });
  },
  search(e) {
    this.setData({
      ["form.sName"]: e.detail.keyWords,
    });
    this.getList();
  },
  async getList() {
    this.setData({
      list: [],
      checkAll: [],
      resultIds: [],
      resultArr: [],
    });
    let res = await WXAPI.getShopCarList(this.data.form);
    if (res.data.code == 0) {
      this.setData({
        list: res.data.data.list,
      });
      let allGoodsIdsArr = [];
      let allGoodsArr = [];
      for (let i = 0; i < res.data.data.list.length; i++) {
        for (
          let j = 0;
          j < res.data.data.list[i].ProductGoodsShoppingCarts.length;
          j++
        ) {
          allGoodsIdsArr.push(
            res.data.data.list[i].ProductGoodsShoppingCarts[j].sId
          );
          allGoodsArr.push(res.data.data.list[i].ProductGoodsShoppingCarts[j]);
        }
      }
      this.setData({
        allGoodsIdsArr,
        allGoodsArr,
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      currentMode: "1",
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      list: [],
    });
    this.getList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
